/* eslint-disable @typescript-eslint/naming-convention */
import { RootContext } from '@/App.context'
import { makeAutoObservable } from 'mobx'
import { IAbility } from 'wts/dist/interface/IAbility'
import { AppOperator } from 'wts/dist/operator/AppOperator'
import { Asset, DesignMethod } from 'wts/dist/schema/Conception'
import { DesignOperator } from 'wts/dist/operator/DesignOperator'
import { ILocalStorage } from 'wts/dist/interface/ILocalStorage'
import { IOwner } from 'wts/dist/interface/IOwner'

export default class WtsStore {
  public context: RootContext
  public app: AppOperator
  public operator: DesignOperator
  public abilityCallbacks: IAbility
  public localStorageOperator: ILocalStorage
  public owner: IOwner

  abilityCallbackTable: Record<string, any[]> = {}

  constructor(context: RootContext) {
    this.context = context
    this.abilityCallbacks = {
      on_add_asset: async (asset: Asset) => {
        const funcs = this.abilityCallbackTable['on_add_asset'] ?? []
        const res = await Promise.all(funcs.map((func) => func(asset)))
        console.log('[WTS] add asset')
        return res
      },
      on_add_method: async (method: DesignMethod) => {
        const funcs = this.abilityCallbackTable['on_add_method'] ?? []
        const res = await Promise.all(funcs.map((func) => func(method)))
        console.log('[WTS] add method')
        return res
      },
      on_update_method: async (method: DesignMethod) => {
        const funcs = this.abilityCallbackTable['on_update_method'] ?? []
        const res = await Promise.all(funcs.map((func) => func(method)))
        console.log('[WTS] update method')
        return res
      },
      on_remove_asset(asset: Asset, methodId: string | null): any {
        return
      },
      on_remove_method(method: DesignMethod): any {
        return
      },
    }
    this.localStorageOperator = {
      get_local_storage: (key: string) => {
        return localStorage.getItem(key)
      },
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      set_local_storage: function (key: string, value: any) {
        const prev_value = localStorage.getItem(key)
        localStorage.setItem(key, value)
        return prev_value
      },
    }
    this.app = new AppOperator(this.localStorageOperator, null, null, true)

    makeAutoObservable(this, {}, { autoBind: true })
  }

  createDesign = async (name: string, image: string) => {
    // TODO: 将image上传为资产，然后将资产id传到image参数(在这个函数里就可)
    const result = await this.app.create_design(
      name,
      null,
      image,
      this.abilityCallbacks
    )
    this.operator = result
  }

  initOperator = async (projectID = 3) => {
    this.operator = await this.app.load_design(projectID, this.abilityCallbacks)
    // this.owner = {
    //   user_token: this.localStorageOperator.get_local_storage('token'),
    //   design_id: Number(this.localStorageOperator.get_local_storage('id')),
    // }
    // this.operator = new DesignOperator(
    //   this.abilityCallbacks,
    //   this.localStorageOperator,
    //   this.owner
    // )
  }

  addEventListener = (event: keyof IAbility, callback: any) => {
    if (this.abilityCallbackTable[event]) {
      this.abilityCallbackTable[event].push(callback)
    } else {
      this.abilityCallbackTable[event] = [callback]
    }
  }

  removeEventListener = (event: keyof IAbility, callback: any) => {
    this.abilityCallbackTable[event] = this.abilityCallbackTable[event].filter(
      (cb) => cb !== callback
    )
  }
}
